package cn.wegoteam.shop.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.stereotype.Controller;

import cn.wegoteam.shop.enu.StoreType;
import cn.wegoteam.shop.inter.ProductServiceInter;
import cn.wegoteam.shop.inter.StoreServiceInter;
import cn.wegoteam.shop.po.Product;
import cn.wegoteam.shop.po.Store;
import cn.wegoteam.shop.util.Const;
import cn.wegoteam.shop.util.DataUtils;

@ParentPackage("default")
@Namespace("/")
@Controller
public class StoreAction extends BaseAction<Store> {
	@Resource(name = "storeService")
	private StoreServiceInter storeService;
	@Resource(name = "productService")
	private ProductServiceInter productService;
	private Store store;
	private Product product;
	private List<Store> stores;

	// 如果用户在商品的详情页面停留5秒，那么就把该商品添加到用户的收藏表中
	@Action(value = "recordProduct")
	public void recordProduct() {
		if (getSessionUser() != null&& getParameter("p_id", 0)!=0) {
			List<Store> stores = storeService.getList(null, DataUtils.getMap(
					"user.id", getSessionUser().getId(), "product.id",
					getParameter("p_id", 0), "type", StoreType.VIEW));
			if (stores.size() < 1) {
				store = new Store();
				Product product = new Product();
				product.setId(getParameter("p_id", 0));
				store.setProduct(product);
				store.setUser(getSessionUser());
				store.setType(StoreType.VIEW);
				storeService.save(store);
			}
		}
	}
	/*
	 *  用户收藏喜欢的商品
	 *  收藏商品时先判断该商品是否是用户之前取消收藏后的商品
	 *  如果是用户收藏后取消收藏的商品，将该商品的标志位置1
	 */
	@Action(value = "collectProduct")
	public void collectProduct() {
		if (getSessionUser() != null&&getParameter("p_id", 0)!=0) {
			stores = storeService.getList(null, DataUtils.getMap(
					"user.id", getSessionUser().getId(), "product.id",
					getParameter("p_id", 0), "type", StoreType.STORE));
			product = new Product(getParameter("p_id", "0"));
			if (stores.size() < 1) {
				store = new Store();
				store.setProduct(product);
				store.setUser(getSessionUser());
				store.setType(StoreType.STORE);
				String result = storeService.save(store);
				if (result.equals(Const.RETURNSUCCESS)) {
					map.put("id", product.getId());
					productService
							.executeByHql(
									"update Product set favorite=favorite+1 where id=:id",map);
					writeStringToResponse("[OK]");
				}
			}else{
				if(stores.get(0).getFlag().equals(1)){
					writeStringToResponse("[COLLECTED]");
				}else{
					map.put("flag", 1);
					map.put("id",stores.get(0).getId());
					storeService.executeByHql("update Store set flag=:flag where id=:id", map);
					productService.executeByHql("update Product set favorite=favorite+1 where id=:id",DataUtils.getMap("id",product.getId()));
					writeStringToResponse("[OK]");
				}
			}
		}else{
			writeStringToResponse("[NO]");
		}
	}
}
